package com.ssm.hyl.aop;

import com.hyl.ssm.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * LogAop
 *
 * @author hyl
 * @date 2024-12-13
 */
public class LogAopAnnotation {

    public void before(JoinPoint jp){
        System.out.println("进入"
                +jp.getTarget()+" "
                + jp.getSignature().getName());
    }

    public void afterResult(JoinPoint jp,Object ret){
        System.out.println("成功,"+"结果"+" " + ret);
    }

    public void error(JoinPoint jp,Throwable e){
        System.out.println("错误："+e.getMessage());
    }

    public void afterFinally(JoinPoint jp){
        System.out.println("最后");
    }


    public void around(JoinPoint jp){
        System.out.println("进入"
                +jp.getTarget()+" "
                + jp.getSignature().getName());
        try {
            //逻辑
            int a = 20, b = 5;
            int ret = a / b;
            System.out.println("成功,"+"结果"+" " + ret);
        }catch (ArithmeticException e){
            System.out.println("错误："+e.getMessage());
        }finally {
            System.out.println("最后");
        }
    }


    public Object around1(ProceedingJoinPoint jp){
        Object ret = null;
        SqlSession session = MyBatisUtil.createSqlSession();
        try {

            //逻辑==>实际请求的方法
            Object[] param = {session};
            ret =   jp.proceed(param);
            session.commit();
        } catch (Throwable e) {
            session.rollback();
        } finally {
           session.close();
        }
        return ret ;
    }
}
